<?php 
define('IN_IBEIKE',true);
define('DIR_ROOT',__DIR__.'/../../');
set_exception_handler(array('core', 'handleException'));
spl_autoload_register(array('core', 'autoload'));
C::createapp();
class core{
	private static $_app;
	private static $_memory;
	private static $_imports;
	private static $_tables;
	public static function autoload($class) {
		$class = strtolower($class);
		if(strpos($class, '_') !== false) { //table_user_user
			list($folder) = explode('_', $class);//table user user
			$file = 'class/'.$folder.'/'.$class;//class/table/table_user_user
		} else {
			$file = 'class/class_'.$class;
		}
		try {
			self::import($file);
			return true;
		} catch (Exception $exc) {
			$trace = $exc->getTrace();
			foreach ($trace as $log) {
				if(empty($log['class']) && $log['function'] == 'class_exists') {
					return false;
				}
			}
		}
	}
	public static function app() {
		return self::$_app;
	}
	public static function createapp(){
		if(!is_object(self::$_app)) {
			self::$_app = common_application::instance();
		}
		return self::$_app;
	}
	public static function t($name) {
		$classname = 'table_'.$name;
		if(!isset(self::$_tables[$classname])) {
			self::$_tables[$classname] = new $classname;
		}
		return self::$_tables[$classname];
	}
	public static function memory() {
		if(!self::$_memory) {
			self::$_memory = new common_memory();
			self::$_memory->init(self::app()->config['memory']);
		}
		return self::$_memory;
	}
	public static function import($name, $folder = '', $force = true) {
		$key = $folder.$name;
		if(!isset(self::$_imports[$key])) {
			$file = DIR_ROOT.'./source/'.$name.'.php';
			
			if(is_file($file)) {
				self::$_imports[$key] = true;
				return include $file;
			} elseif(!$force) {
				return false;
			} else {
				throw new Exception('Oops! System file lost: '.$file);
			}
		}
		return true;
	}
	public static function handleException($exception){//异常处理入口
		common_error::exception_error($exception);
	}
	public static function handleError($errno, $errstr, $errfile, $errline) {
		if($errno) {
			common_error::system_error($errstr, false, true, false);
		}
	}
	public static function handleShutdown() {
		if(($error = error_get_last()) && $error['type']) {
			common_error::system_error($error['message'], false, true, false);
		}
	}
}

class C extends core {}
class DB extends common_database {}

?>